package com.geeklan.smart.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.geeklan.smart.R;
import com.geeklan.smart.utils.DensityUtils;
import com.geeklan.smart.utils.FloatUtils;

public class HorizontalRulerView extends View {

    private Paint linePaint;
    private Paint markPaint;
    private Paint textPaint;
    private Paint selectedPaint;

    private float selectedPosition = 0;  // 当前选中的刻度位置
    private int scaleWidth = 20;      // 每个刻度的宽度
    private float offsetX = 0;        // 当前的偏移量（用于拖动）

    private float lastTouchX;         // 上次触摸的 X 坐标

    private int startValue = 0;      // 自定义起始值
    private int endValue = 100;       // 自定义终止值
    private float step = 0.1f;             // 每个刻度之间的间隔
    private int bottom = 0;
    private Context mContext;
    public HorizontalRulerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        bottom = DensityUtils.dpToPx(context,50);
        init();
    }

    private void init() {
        // 画线的画笔
        linePaint = new Paint();
        linePaint.setColor(getContext().getColor(R.color.color_9ccc9c));
        linePaint.setStrokeWidth(4);

        // 刻度的画笔
        markPaint = new Paint();
        markPaint.setColor(getContext().getColor(R.color.color_9ccc9c));
        markPaint.setStrokeWidth(4);
        markPaint.setTextSize(30);
        markPaint.setTextAlign(Paint.Align.CENTER);

        // 文字
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setStrokeWidth(4);
        textPaint.setTextSize(DensityUtils.dpToPx(getContext(),14));
        textPaint.setTextAlign(Paint.Align.CENTER);

        // 文字
        selectedPaint = new Paint();
        selectedPaint.setColor(Color.BLACK);
        selectedPaint.setStrokeWidth(4);
        selectedPaint.setTextSize(DensityUtils.dpToPx(getContext(),20));
        selectedPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = getHeight();
        int width = getWidth();
        int centerX = width / 2;

        // 计算总刻度数量
        float scaleCount = (endValue - startValue) / step;

        float startX = centerX - selectedPosition * scaleWidth + (int) offsetX;
        float endX = startX + scaleCount * scaleWidth;

        startX = Math.max(startX, 0);
        endX = Math.min(endX, width);

        canvas.drawLine(startX, height - bottom, endX, height - bottom, linePaint);

        // 画刻度
        for (int i = 0; i <= scaleCount; i++) {
            float x = centerX + (i - selectedPosition) * scaleWidth + (int) offsetX;

            if (x < 0 || x > width) continue; // 超出屏幕的部分不绘制

            int markHeight;
            if ((startValue + i * step) % 1 == 0) {
                markHeight = 60; // 10 的倍数刻度更长
                markPaint.setStrokeWidth(5);
            } else if ((startValue + i * step) % 0.5f == 0) {
                markHeight = 50; // 5 的倍数刻度稍长
                markPaint.setStrokeWidth(5);
            } else {
                markHeight = 40; // 普通刻度
                markPaint.setStrokeWidth(4);
            }

            canvas.drawLine(x, height - bottom, x, height - bottom - markHeight, markPaint);

            // 绘制刻度文字
            if ((startValue + i * step) % 1 == 0) {
                canvas.drawText(String.valueOf((int) (startValue + i * step)), x, height - (bottom / 3), textPaint);
            }
        }
        canvas.drawText(FloatUtils.roundTo1DecimalUsingBigDecimal(startValue + selectedPosition * step) + "kg", centerX, height - bottom - 150, selectedPaint);
        // 绘制中间的选中竖线
        drawCenterLine(canvas, centerX, height);
    }

    private void drawCenterLine(Canvas canvas, int centerX, int height) {
        int centerLineHeight = 100; // 中心竖线的长度
        markPaint.setStrokeWidth(5);
        canvas.drawLine(centerX, height - bottom, centerX, height - bottom - centerLineHeight, markPaint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastTouchX = event.getX();
                return true;
            case MotionEvent.ACTION_MOVE:
                float currentX = event.getX();
                offsetX += currentX - lastTouchX; // 累加拖动的偏移量
                lastTouchX = currentX;
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // 松开手指后，计算最近的刻度
                selectedPosition -= Math.round(offsetX / scaleWidth);
                offsetX = 0; // 重置偏移量

                // 限制选中的位置在范围内
                float scaleCount = (endValue - startValue) / step;
                if (selectedPosition < 0) selectedPosition = 0;
                if (selectedPosition > scaleCount) selectedPosition = scaleCount;

                invalidate(); // 重新绘制
                return true;
        }
        return super.onTouchEvent(event);
    }

    // 设置自定义范围
    public void setRange(int startValue, int endValue, int step) {
        this.startValue = startValue;
        this.endValue = endValue;
        this.step = step;
        this.selectedPosition = 0; // 重置选中位置
        this.offsetX = 0; // 重置偏移量
        invalidate();
    }

    // 获取当前选中的刻度值
    public float getSelectedValue() {
        return (startValue + selectedPosition * step);
    }
}
